﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class UpdViewModel:BaseViewModel<UpdViewModel>
    {

        #region Variabless
        List<string> listIP = new List<string>();
        List<string> listPatientID = new List<string>();


        #endregion

        #region Constructor
        public UpdViewModel()
        {
            findAllIP();
        }
        #endregion

        #region Properties


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        public void findAllIP()
        {
            foreach (ConfigOperationRoomModel item in ConfigOperationRoomModelService.Instance.FindAll())
            {
                if (!string.IsNullOrEmpty(item.Ips))
                {
                    string[] ips = item.Ips.Split('|');
                    foreach (string ip in ips)
                    {
                        if (IsValidIP(ip.ToString()))
                        {
                            listIP.Add(ip.ToString());
                        }
                    }
                }
            }
        }
        //查询所有病人ID
        public void findAllPatientID()
        {
            listPatientID.Clear();
            foreach (ConfigOperationRoomModel item in ConfigOperationRoomModelService.Instance.FindAll())
            {
                if (!string.IsNullOrEmpty(item.PatientLinkId.ToString()))
                   {
                      listPatientID.Add(item.PatientLinkId.ToString());
                   }
            }
        }

        //存IP
        private void UpdateIPs()
        {
            IPAddress[] ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            string ips = "";
            foreach (IPAddress item in ip)
            {
                if (IsValidIP(item.ToString()))
                {
                    ips += item.ToString()+"|";
                }
            }
            DataManager.Instance.CurRoom.Ips = ips;
        }



        //判断是否是IPV4
        private  bool IsValidIP(string ip)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
            {
                string[] ips = ip.Split('.');
                if (ips.Length == 4 || ips.Length == 6)
                {
                    if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                        return true;
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
            else
                return false;
        }
        private void Update()
        {
            UpdateIPs();
            DataManager.Instance.CurRoom.LoginStatus = "1";
            DataManager.Instance.CurRoom.PatientLinkId = DataManager.Instance.CurrentPatientLink.Id;
            DataManager.Instance.CurRoom.AccountLinkId = DataManager.Instance.Login.Id;
            ConfigOperationRoomModelService.Instance.Update(DataManager.Instance.CurRoom);
        }

        #endregion

        #region Public/Protected Methods

        public void SendMsgByIP(string msg)
        {
            UDPHelper.Instance.SendMsg(msg);
            //Update();
            //foreach (string item in listIP)
            //{
            //    UDPHelper.Instance.IpStr = item;
            //    UDPHelper.Instance.SendMsg(msg);
            //}
        }

        public void UpdataByID()
        {

            if (!string.IsNullOrEmpty(DataManager.Instance.Result))
            {
                string[] sts = DataManager.Instance.Result.Split('|');
                string[] sts1 = sts[0].Split('_');
                string modelName = sts1[0];
                string field = sts1[1];
                string patientLinkId = sts[2];
                string value = sts[1];
                if (modelName.Equals("PatientModel") && patientLinkId.Equals(DataManager.Instance.CurrentPatientLink.Id.ToString())
                    && !value.Equals(DataManager.Instance.CurrentPatientLink.Patient.GetType().GetProperty(field).GetValue(DataManager.Instance.CurrentPatientLink.Patient)))
                {
                    DataManager.Instance.CurrentPatientLink.Patient.GetType().GetProperty(field).SetValue(DataManager.Instance.CurrentPatientLink.Patient, value);
                }
                //else if (modelName.Equals("Asa") && patientLinkId.Equals(DataManager.Instance.CurrentPatientLink.Id.ToString()))
                //{
                //    DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Asa.GetType().GetProperty(field).SetValue(DataManager.Instance.CurrentPatientLink.Patient, value);
                //}else
                //if (modelName.Equals("AnesthesiaRecord") && patientLinkId.Equals(DataManager.Instance.CurrentPatientLink.Id.ToString()))
                //{
                //    DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.GetType().GetProperty(field).SetValue(DataManager.Instance.CurrentPatientLink.Patient, value);
                //}
            }


            //DataManager.Instance.CurRoom =ConfigOperationRoomModelService.Instance.GetObjById(
            //    DataManager.Instance.CurRoom.Id.ToString());
            //findAllPatientID();
            //if (listPatientID.Where(O => O.Equals(DataManager.Instance.CurrentPatientLink.Id.ToString())).Count() > 0)
            ////
            ////DataManager.Instance.CurrentPatientLink.Id.Equals(DataManager.Instance.CurRoom.PatientLinkId))
            //{
            //    DataManager.Instance.CurrentPatientLink = OperationDataPool.Instance.GetCurOper(DataManager.Instance.CurrentPatientLink.Id.ToString());
            //    PatientLink = DataManager.Instance.CurrentPatientLink;
            //}

        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
